<?php
//vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldcolumn=1 foldmethod=marker:
/**
 * Project Name:  5anet
 * File Name:  class/index/ShowIndex.class.php
 *
 * 显示本站的首页
 *
 * PHP Version 5
 *
 * @package:   class.index
 * @author:    Mike.G   Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: http://www.5anet.com
 * @version:   $Id: ShowIndex.class.php,v 1.1 2006-09-02 17:05:33 ghw Exp $
 * @Date:      $Date: 2006-09-02 17:05:33 $
 */

include_once CLASS_PATH.'main/BaseAction.class.php';
include_once CLASS_PATH.'post/PostUtil.class.php';
include_once CLASS_PATH.'user/UserUtil.class.php';


include_once FUNCTION_PATH.'utf8_substr.fun.php';

//include the language resource file
if ( file_exists(LANG_PATH.SYSTEM_LANG.'/ShowIndex.lang.php') ) {
   include_once LANG_PATH.SYSTEM_LANG.'/ShowIndex.lang.php';
}


class ShowIndex extends BaseAction {
   /**
    * 数据库的连接
    */
   private $db;



   /**
    * 构造函数
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function __construct() {
      $this->db = $this->getDB();
   }


   
   /**
    * 显示首页的信息
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function run() {
      $smarty = $this->getSmarty();

      $is_have_post = false;
      if ( PostUtil::haveNotExpirePost($this->getDB()) ) {
         $is_have_post = true;
         $post_array = PostUtil::getPost($this->getDB(), 3);

         foreach ( $post_array as $post_rows ) {
            $post_str .= '<a href=\'index.php?module=post&action=view&id='.
               $post_rows['id'].'\' title=\''.$post_rows['title'].'\'>'.
               utf8_substr($post_rows['title'], 0, 35).'</a>'.
               '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
         }
      }

      //处理日历区
      //

      $now = getdate();
      $year = $now['year'];
      $month = $now['mon'];
      $day = $now['mday'];
      $smarty->assign('nowyear', $year);
      $smarty->assign('nowmon', $month);
      $smarty->assign('nowday', $day);

      //生成年份和月份的选择框
      $year_option = "";

      for($i=2005; $i<2015; $i++ ) {
         $year_option .= "<option value=$i ";
         if ( $i == $year ) {
            $year_option .= " selected ";
         }
         $year_option .= ">".$i."</option>\n";
      }

      $smarty->assign('year_option', $year_option);


      //生成月份的选择框
      $month_option = "";
      for ( $i=1; $i<=12; $i++ ) {
         $month_option .= "<option value=$i ";

         if ( $i == $month ) {
            $month_option .= " selected ";
         }

         $month_option .= ">".$i."</option>\n";
      }

      $smarty->assign('month_option', $month_option);


      $smarty->assign('have_system_post', $is_have_post);
      $smarty->assign('post_str', $post_str);


      //取得当前的计数器和
      //当前在线的人数
      $sql = 'select total_count from total_count where id=1';
      $res = $this->db->CacheExecute(120, $sql);
      $rows = $res->FetchRow();
      $smarty->assign('web_count', number_format($rows['total_count']));

      //当前在线人数
      $sql = 'select count(*) as num from online_user';
      $res = $this->db->CacheExecute(60, $sql);
      $rows = $res->FetchRow();
      $smarty->assign('online_user_number', number_format($rows['num']));
      
      //今日已经访问的用户数
      $sql = 'select access_number as num from web_count where count_date=?';
      $res = $this->db->CacheExecute(120, $sql, array($year.'-'.$month.'-'.$day));
      $rows = $res->FetchRow();
      $smarty->assign('had_access_number', number_format($rows['num']));

      //新注册的用户
      $sql = 'select id, user_name, user_header from base_user_info order by id desc ';
      $res = $this->db->CacheSelectLimit(120, $sql, 6, 0);

      $user_info = array();

      while ( $rows = $res->FetchRow() ) {
         $user_info[] = array(
            'id'=>$rows['id'],
            'user_name'=>utf8_substr($rows['user_name'], 0, 4),
            'name'=>$rows['user_name'],
            'user_header'=>UserUtil::getUserHeader($this->db, $rows['id'])
         );
      }

      $smarty->assign('new_user_info', $user_info);


      //文章阅读排行榜
      $sql = 'select id, title,clicks from article order by clicks desc, id desc ';
      $res = $this->db->CacheSelectLimit(160, $sql, 10, 0);
      $article = array();

      while ( $rows = $res->FetchRow() ) {
         $article[] = array (
            'id'=>$rows['id'],
            'name'=>$rows['title'],
            'sname'=>utf8_substr($rows['title'], 0, 9)."<font color=red>(".
               $rows['clicks'].")</font>",
         );
      }
      $smarty->assign('article_sort', $article);


      //软件下载排行
      $sql = 'select id, name, downloads from soft order by downloads desc, id desc ';
      $res = $this->db->CacheSelectLimit(360, $sql, 10, 0);
      $soft = array();

      while ( $rows = $res->FetchRow() ) {
         $soft[] = array(
            'id'=>$rows['id'],
            'name'=>$rows['name'],
            'sname'=>utf8_substr($rows['name'], 0, 15)."<font color=red>(".
                  $rows['downloads'].")</font>",
         );
      }

      $smarty->assign('soft_sort', $soft);


      //相册排行
      $sql = 'select id, name, image_type, user_id from album order by clicks desc, id desc';
      $res = $this->db->CacheSelectLimit(360, $sql, 10, 0);
      $album = array();

      while ( $rows = $res->FetchRow() ) {
         $album[] = array(
            'id'=>$rows['id'],
            'name'=>$rows['name'],
            'img'=>ROOT_URL.'upload/album/'.$rows['user_id'].'/'.
            $rows['id'].'-117-83.'.$rows['image_type']
         );
      }

      $smarty->assign('album_sort', $album);


      //论坛热帖
      $sql = 'select id, title from bbs_subject order by reply_number desc, id desc ';
      $res = $this->db->CacheSelectLimit(360, $sql, 10, 0);
      $bbs = array();

      while ( $rows = $res->FetchRow() ) {
         $bbs[] = array(
            'id'=>$rows['id'],
            'title'=>$rows['title'],
            'stitle'=>utf8_substr($rows['title'], 0, 10),
         );
      }

      $smarty->assign('bbs_sort', $bbs);


      //最新文章
      $sql = 'select id, title,clicks from article where type=110 or type=96 order by id desc ';
      $res = $this->db->CacheSelectLimit(160, $sql, 13, 0);
      $new_article = array();

      while ( $rows = $res->FetchRow() ) {
         $new_article[] = array (
            'id'=>$rows['id'],
            'name'=>$rows['title'],
            'sname'=>utf8_substr(trim($rows['title']), 0, 22)."<font color=red>(".
               $rows['clicks'].")</font>",
         );
      }
      $smarty->assign('new_article', $new_article);

      $sql = 'select id, title,clicks from article where type!=110 and type!=96 order by id desc ';
      $res = $this->db->CacheSelectLimit(160, $sql, 13, 0);
      $new_article2 = array();

      while ( $rows = $res->FetchRow() ) {
         $new_article2[] = array (
            'id'=>$rows['id'],
            'name'=>$rows['title'],
            'sname'=>utf8_substr(trim($rows['title']), 0, 22)."<font color=red>(".
               $rows['clicks'].")</font>",
         );
      }
      $smarty->assign('new_article2', $new_article2);

      //最新的相片
      $sql = 'select id, name, size, image_type, user_id from album order by id desc';
      $res = $this->db->CacheSelectLimit(240, $sql, 12, 0);
      $new_album = array();

      while ( $rows = $res->FetchRow() ) {
         $new_album[] = array(
            'id'=>$rows['id'],
            'name'=>$rows['name'],
            'sname'=>utf8_substr($rows['name'], 0, 6),
            'size'=>$rows['size'],
            'img'=>ROOT_URL.'upload/album/'.$rows['user_id'].'/'.
            $rows['id'].'-117-83.'.$rows['image_type']
         );
      }

      $smarty->assign('new_album', $new_album);

      //最新软件下载
      $sql = 'select id, name, version, website, downloadurl, comment, downloads from soft ';
      $sql .= ' order by id desc limit 10 offset 0';
      $res = $this->db->CacheExecute(120, $sql);

      $soft_list = array();
      while ( $rows = $res->FetchRow() ) {

         $temp_sql = 'select image from soft_image where soft=? order by id asc';
         $temp_res = $this->db->CacheSelectLimit(120, $temp_sql, 1, 0, array($rows['id']));
         $temp_rows = $temp_res->FetchRow();

         $img = $temp_rows['image']?ROOT_URL.'upload/soft/image/'.$temp_rows['image']:IMAGE_URL.'softdefault.jpg';
         $soft_list[] = array (
            'id'=>$rows['id'],
            'name'=>$rows['name'],
            'sname'=>utf8_substr($rows['name'], 0, 20),
            'version'=>$rows['version'],
            'scomment'=>ConvertString(utf8_substr($rows['comment'], 0, 20), ROOT_URL, IMAGE_URL.'express/'),
            'downloads'=>$rows['downloads'],
            'img'=>$img
            );
      }

      $smarty->assign('new_soft', $soft_list);

      //
      //论坛新帖
      $sql = 'select id, title from bbs_subject order by last_access_date desc, id desc ';
      $res = $this->db->CacheSelectLimit(60, $sql, 26, 0);
      $new_bbs = array();

      while ( $rows = $res->FetchRow() ) {
         $new_bbs[] = array(
            'id'=>$rows['id'],
            'title'=>$rows['title'],
            'stitle'=>utf8_substr($rows['title'], 0, 24),
         );
      }

      $smarty->assign('new_bbs', $new_bbs);


      //友情连接
      $sql = 'select href, name, logo_url, width, height from links order by id asc ';
      $res = $this->db->CacheExecute(1, $sql);

      $links = array();
      while ( $rows = $res->FetchRow() ) {
         $links[] = array(
            'href'=>$rows['href'],
            'name'=>$rows['name'],
            'logo_url'=>$rows['logo_url'],
            'width'=>$rows['width'],
            'height'=>$rows['height'],
         );
      }

      $smarty->assign('mylink', $links);

     





      $smarty->display("showindex.tmpl");

   }
 


}


?>
